package com.example.demo.param.service.impl;


import com.example.demo.common.utils.Constant;
import com.example.demo.information.entity.School;
import com.example.demo.param.entity.Param;
import com.example.demo.param.repository.ParamRepository;
import com.example.demo.param.service.ParamService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ParamServiceImpl  implements ParamService {

    @Autowired
    private ParamRepository paramRepository;

    @Override
    public Map<Long, Param> findSchoolPhotoParamMap() {
        Map<Long,Param> map = new HashMap<>();
        List<Param> paramList = paramRepository.findByPzsm(Constant.PARAM_PZSM_PHOTO);
        for(Param param :paramList){
            map.put(param.getXxxxId(),param);
        }
        return map;
    }

    @Override
    public List<Map<String, Object>> loadSchoolPhotoParamJson(List<School> schoolList) {
        List<Map<String,Object>> list = new ArrayList<>();
        Map<Long, Param> schoolPhotoParamMap = findSchoolPhotoParamMap();//Map<xxId,PhotoParam>
        String spidStrDefault = "未设置(采用默认)";
        String bkColorStrDefault = "未设置(采用默认)";
        for(School school:schoolList){
            Map<String,Object> paramTempMap = new HashMap<>();
            paramTempMap.put("xxId",school.getId());
            paramTempMap.put("xxmc",school.getName());
            paramTempMap.put("xxmc_short",school.getShort_name());
            Param param = schoolPhotoParamMap.get(school.getId());
            if(param!=null){
                String spidStr = Constant.photoParamSpidMap.get(param.getPzz());
                if(StringUtils.isNotBlank(spidStr)){
                    String[] split = spidStr.split("@@");
                    spidStr = split[0]+"("+split[1]+")";
                }else{
                    spidStr = spidStrDefault;
                }
                String bkColorStr = Constant.photoParamBkColorMap.get(param.getPzz2());
                bkColorStr = StringUtils.isNotBlank(bkColorStr)?bkColorStr:bkColorStrDefault;
                paramTempMap.put("spid",param.getPzz());
                paramTempMap.put("bkColor",param.getPzz2());
                paramTempMap.put("spidStr", spidStr);
                paramTempMap.put("bkColorStr", bkColorStr);
            }else{
                paramTempMap.put("spid","");
                paramTempMap.put("bkColor","");
                paramTempMap.put("spidStr", spidStrDefault);
                paramTempMap.put("bkColorStr", bkColorStrDefault);
            }
            list.add(paramTempMap);
        }
        return list;
    }

    @Override
    public Param findSchoolPhotoParam(Long xxId) {
        return paramRepository.findByXxxxIdAndPzsm(xxId,Constant.PARAM_PZSM_PHOTO);
    }

    @Override
    public Param findByPzsmAndXxxxIdNull() {
        return paramRepository.findByPzsmAndXxxxIdNull(Constant.PARAM_PZSM_PHOTO);
    }

    @Override
    public void savePhotoParam(Param param, Long xxId, String spid, String bkColor, String xgr) {
        Date date = new Date();
        String xgsj = Constant.formatter.get().format(date);
        if(param!=null){
            param.setXgr(xgr);
            param.setXgsj(xgsj);
        }else{
            param = new Param();
            param.setPzsm(Constant.PARAM_PZSM_PHOTO);
            if(xxId!=null){
                param.setXxxxId(xxId);
            }
            param.setLrr(xgr);
            param.setLrsj(xgsj);
        }
        param.setPzz(spid);
        param.setPzz2(bkColor);
        paramRepository.save(param);
    }
}
